package 算法.leetcode.stack;

import org.junit.Test;

import java.util.Stack;

/**
 * @author lchenglong
 * @date 2021/1/11
 */
public class BackspaceCompare {
    @Test
    public void test() {
        String S = "y#fo##f";
        String T = "y#f#o##f";
        System.out.println(backspaceCompare(S,T));
    }
    public boolean backspaceCompare(String S, String T) {
        Stack<Character> s = new Stack<Character>();
        Stack<Character> t = new Stack<Character>();
        for (int i=0;i<S.length();i++) {
            Character c = S.charAt(i);
            if (c.equals('#')) {
                if (!s.empty()) s.pop();
            } else {
                s.push(c);
            }
        }
        for (int i=0;i<T.length();i++) {
            Character c = T.charAt(i);
            if (c.equals('#')) {
                if (!t.empty())t.pop();
            } else {
                t.push(c);
            }
        }

        StringBuilder ss = new StringBuilder();
        while (!s.empty()){
            ss.append(s.pop());
        }
        StringBuilder tt = new StringBuilder();
        while (!t.empty()){
            tt.append(t.pop());
        }
        return ss.toString().equals(tt.toString());

    }
}
